package com.yellowpage.bigdata.service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.bigdata.data.TaO2oChannelDay;
import com.yellowpage.bigdata.dao.TaO2oChannelDayDao;

public class TaO2oChannelDayServiceImpl extends BaseServiceImpl implements TaO2oChannelDayService {
	
	private TaO2oChannelDayDao taO2oChannelDayDao;

	public void setTaO2oChannelDayDao(TaO2oChannelDayDao taO2oChannelDayDao) {
		this.taO2oChannelDayDao = taO2oChannelDayDao;
	}

	/* Smooth generate Method */
	public TaO2oChannelDay getTaO2oChannelDay(Long id) {
		return taO2oChannelDayDao.get(id);
	}

	public List<TaO2oChannelDay> getAllTaO2oChannelDay() {
		return taO2oChannelDayDao.getAll();
	}

	// 做一个内部调用函数，简化代码
	private QueryAssistantSQL returnsqlAssistant(Map queryMap) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						
						"SELECT A.DAY_ID,A.CHANNEL_NO,B.`NAME` AS CHANNEL_NAME,A.YES_UV,A.UV,A.YES_PV,A.PV,A.TOTAL_UV,A.TOTAL_PV"
								+ " FROM TA_O2O_CHANNEL_DAY A"
								+ " JOIN OMC_DIC_TABLE B ON B.VALUE = A.CHANNEL_NO AND B.DIC_NAME = 'O2O_LOG_CHANNEL_NO'")
								
				.addCondition(" and A.DAY_ID >= :dayId ")
				.addCondition(" and A.DAY_ID <= :etlTime ")
				.addCondition(" and A.CHANNEL_NO = :channelNo ");

		return sqlAssistant;
	}

	//分页查询
	@Override
  	public List<Map> queryTaO2oChannelDayByPage(Map queryMap,
			PageRender pageRender)  {

 		QueryAssistantSQL sqlAssistant = returnsqlAssistant(queryMap);

 		List<Map> resultList = sqlAssistant.queryByPage(
 				baseDao.getConnection(), queryMap, pageRender);

 		return resultList;
 	}

    @Override
	public List<Map> queryTaO2oChannelDayByPage(Map queryMap) {

		QueryAssistantSQL sqlAssistant = returnsqlAssistant(queryMap);

		// 返回全量
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				queryMap);

		return resultList;
	}
    
	@Override
	public Map getO2oChannelMap() {
		Map dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" SELECT a.name as NAME, a.VALUE as VALUE  FROM OMC_DIC_TABLE a where dic_name='O2O_LOG_CHANNEL_NO' ");
		sqlAssistant.setTail("order by (a.VALUE+0)");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("VALUE") + "-" + resultList.get(i).get("NAME"));
		}
		return dataSourceMap;
	}

}	